import 'dart:math';

import 'package:get/get.dart';
import 'package:shop/base/sc/load_state_model.dart';

// VM
abstract class StateController extends DisposableInterface {
  /// 数据状态
  final Rx<LoadStateType> loadState = LoadStateType.initial().obs;

  /// 刷新数据
  Future<void> refreshData() async {
    throw UnimplementedError();
  }
}

abstract class ModelSC<T> extends StateController {
  final Rxn<T> data = Rxn<T>();

  /// 请求数据
  Future<T?> loadData() async {
    throw UnimplementedError();
  }

  /// 刷新数据
  @override
  Future<void> refreshData() async {
    try {
      loadState.value = LoadStateType.refreshIng();
      final t = await loadData();
      data.value = t;
      loadState.value = LoadStateType.refreshSuccess();
      // 如果没有数据
    } catch (e) {
      loadState.value = LoadStateType.refreshError();
      rethrow;
    }
  }
}

/// 列表
abstract class ListSC<T> extends StateController {
  /// 列表的数据
  final RxList<T> data = RxList<T>();

  /// 当前页面
  final RxInt curPage = 0.obs;

  /// 列表页数
  final RxInt totalPage = 0.obs;

  /// 数据库上的总数量,非当前列表内的数量
  int? _count;
  set count(int? value) {
    if (value != null) {
      totalPage.value = (max(value, 0) / limit).ceil();
    }
    _count = value;
  }

  int? get count => _count;

  /// 每页数量
  int limit = 20;
  // int limit = kDebugMode ? 3 : 20;

  /// 页数
  int _page = 0;
  set page(int value) {
    curPage.value = value;
    _page = value;
  }

  int get page => _page;

  /// 请求数据
  Future<List<T>> loadData() async {
    throw UnimplementedError();
  }

  /// 刷新数据
  @override
  Future<void> refreshData() async {
    page = 1;
    try {
      loadState.value = LoadStateType.refreshIng();
      final t = await loadData();
      data.clear(); // 清空数据
      data.addAll(t); //数据添加到数组中
      loadState.value = LoadStateType.refreshSuccess();
      //如果没有数据
    } catch (e) {
      loadState.value = LoadStateType.refreshError();
      rethrow;
    }
  }
}

/// 具备分页能力
abstract class PagingListSC<T> extends ListSC<T> {
  /// 更多数据
  Future<void> loadMoreData() async {
    page++;
    try {
      loadState.value = LoadStateType.loadMoreIng();
      final t = await loadData();
      data.addAll(t); //数据添加到数组中
      bool hasMore;
      // 如果有count,则根据count判断是否还有更多数据
      if (count != null) {
        if (data.length >= count!) {
          hasMore = false;
        } else {
          hasMore = true;
        }
      }
      // 如果没有count,则根据当前数据长度判断是否还有更多数据(注意:这种是不可靠的,如果请求数据的业务逻辑中,过滤了一些数据,会导致判断错误,所以尽量使用 count 参数)
      else {
        if (t.length < limit) {
          hasMore = false;
        } else {
          hasMore = true;
        }
      }

      loadState.value = LoadStateType.loadMoreSuccess(hasMore: hasMore);
      //如果没有数据
    } catch (e) {
      page--;
      loadState.value = LoadStateType.loadMoreError();
      rethrow;
    }
  }

  /// 加载指定页码数据
  Future<void> loadPageData(int page) async {
    this.page = page;
    try {
      loadState.value = LoadStateType.loadMoreIng();
      final t = await loadData();
      data.clear(); // 清空数据
      data.addAll(t); //数据添加到数组中
      loadState.value = LoadStateType.refreshSuccess();
      //如果没有数据
    } catch (e) {
      loadState.value = LoadStateType.loadMoreError();
      rethrow;
    }
  }
}
